package com.financing.platform.task;

import java.sql.SQLException;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.financing.platform.dao.ICountInfoDao;
import com.financing.platform.dto.CountInfoDTO;
import com.financing.platform.util.Dict;
import com.financing.platform.util.StringUtil;

/**
 * 
 * @ClassName: CountInfoJob
 * @Description: 运营统计信息 
 * @author  qiye
 * @date 2014年10月31日 上午10:58:44
 *
 */
@Component("countInfoJob")
public class CountInfoJob {
	
	private Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private ICountInfoDao countInfoDaoImpl;
	
	@Scheduled(cron = "0 05 00 * * ?") 
	public void countInfoJob(){
		log.info("auto count info .....");
		
		CountInfoDTO countInfoDTO = new CountInfoDTO();
		
		countInfoDTO.setCountDate(StringUtil.rollDay(new Date(), -1));
		countInfoDTO.setStatus(Dict.status_normal);
		countInfoDTO.setCreateBy("SYSTEM");
		countInfoDTO.setUpdateBy("system");
		countInfoDTO.setCreateDate(new Date());
		countInfoDTO.setUpdateDate(new Date());
		
		Integer countInfoId = null;
		while( countInfoId == null){
			try{
				countInfoId =  countInfoDaoImpl.addCountInfo(countInfoDTO);
			}catch(SQLException e){
				log.error("add count info error!",e);
			}
		}
		
		countInfoDTO.setId(countInfoId);
		while(!countInfo(countInfoDTO));
		
	}
	
	/**
	 * 
	 * @Title: countInfo
	 * @Description: 统计运营信息 
	 * @param @param countInfoDTO
	 * @param @return   
	 * @return boolean   
	 * @throws
	 */
	private boolean countInfo(CountInfoDTO countInfoDTO){
		try {
			countInfoDTO.setRegistPersonCount(countInfoDaoImpl.registPersonCount());
			countInfoDTO.setRegistVerifyNoPass(countInfoDaoImpl.registVerifyNoPass());
			countInfoDTO.setRegistVerifyPass(countInfoDaoImpl.registVerifyPass());
			countInfoDTO.setRegistVerifyNoPass(countInfoDaoImpl.registVerifyNoPass());
			countInfoDTO.setOrderPerSuccessPerson(countInfoDaoImpl.orderPerSuccessPerson());
			countInfoDTO.setOrderPerFailPerson(countInfoDaoImpl.orderPerFailPerson());
			countInfoDTO.setOrderRebateSuccessPerson(countInfoDaoImpl.orderRebateSuccessPerson());
			countInfoDTO.setOrderCancelPerson(countInfoDaoImpl.orderCancelPerson());
			countInfoDTO.setOrderPerSuccess(countInfoDaoImpl.orderPerSuccess());
			countInfoDTO.setOrderPerFail(countInfoDaoImpl.orderPerFail());
			countInfoDTO.setProductOrderRebateSuccess(countInfoDaoImpl.productOrderRebateSuccess());
			countInfoDTO.setProductOrderCancel(countInfoDaoImpl.productOrderCancel());
			countInfoDTO.setOrderPerPerson(countInfoDaoImpl.orderPerPerson());
			countInfoDTO.setOrderPer(countInfoDaoImpl.orderPer());
			countInfoDTO.setForWardNum(countInfoDaoImpl.forWardNum());
			countInfoDaoImpl.updateCountInfo(countInfoDTO);
			return true;
		} catch (SQLException e) {
			log.error("countInfoJob error !",e);
			return false;
		}
	}
	
}
